package ru.tennisworld.user.controllers;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.transaction.annotation.Transactional;
import ru.tennisworld.user.UserServiceApplication;
import ru.tennisworld.user.dto.RegistrationTennisUserRequestDto;

@SpringBootTest(
        webEnvironment = SpringBootTest.WebEnvironment.MOCK,
        classes = UserServiceApplication.class)
@AutoConfigureMockMvc
@TestPropertySource(
        locations = "classpath:application.yml")
class TennisUserControllerTest {

    @Autowired
    private MockMvc mockMvc;

    private static final ObjectMapper mapper = new ObjectMapper();

    @Test
    @Transactional
    void registrationUser() throws Exception {
        RegistrationTennisUserRequestDto request = new RegistrationTennisUserRequestDto();
        request.setUserLogin("lisic4kin");
        request.setUserPassword("qwerty");
        mockMvc.perform(
                MockMvcRequestBuilders.post("/user/registration")
                        .contentType(MediaType.APPLICATION_JSON).characterEncoding("utf-8")
                        .content(mapper.writeValueAsString(request))
                        .accept(MediaType.APPLICATION_JSON))
                .andExpect(MockMvcResultMatchers.status().is5xxServerError());
    }
}